package cn.dlc.com.server;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.dlc.com.config.Config;
import cn.dlc.com.config.ConfigCenter;
import cn.dlc.com.property.JdbcProperty;
import cn.dlc.com.property.ZookeeperProperty;

/**
 * 容器在初始化的时候直接将这个config就创建好了。
 * 并且往里面写入了具体的配置文件
 * @author Administrator
 *
 */
@Configuration
@EnableConfigurationProperties({JdbcProperty.class, ZookeeperProperty.class})
public class ServerConfig {
	
	@Bean
	public ConfigCenter configCenter(JdbcProperty jdbc, ZookeeperProperty zk) throws Exception{
		ConfigCenter configCenter = new ConfigCenter();
		
		Config config = new Config();
		// 设置基本的信息
		config.setDbUrl(jdbc.getUrl());
		config.setDriverClass(jdbc.getDriverClassName());
		config.setUsername(jdbc.getUsername());
		config.setPassword(jdbc.getPassword());
		
		// 在服务器初始化一个配置节点
		configCenter.initConfig(zk.getUrl(), zk.getNodePath(), config);
		
		return configCenter;
	}
	
}
